package com.ssps.back.platform.service.Impl;

import com.ssps.back.platform.service.LogService;
import com.ssps.entity.*;
import com.ssps.mapper.LogMapper;
import com.ssps.mapper.UserInfoMapper;
import com.ssps.utils.MyLog;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author li
 * @time 2020/07/24 15:20:20
 */

@Aspect
@Component
@Slf4j
public class LogServiceImpl implements LogService {
    @Autowired
    LogMapper logMapper;

    @Autowired
    UserInfoMapper userInfoMapper;
    @Pointcut("@annotation(com.ssps.utils.MyLog)")
    public void logPoinCut() {
    }

    //切面 配置通知
    @AfterReturning("logPoinCut()")
    public void saveSysLog(JoinPoint joinPoint) {
        System.out.println("切面。。。。。");
        //保存日志
        Log log = new Log();
        //从切面织入点处通过反射机制获取织入点处的方法
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获取切入点所在的方法
        Method method = signature.getMethod();

        //获取请求的类名
        String className = joinPoint.getTarget().getClass().getName();
        //获取请求的方法名
        String methodName = method.getName();
        log.setMethods(className + "." + methodName);

        log.setDotime(new Date());
        //获取用户名
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        //读取session中的用户
        User user= (User) session.getAttribute("loginUser");
        UserInfoExample example=new UserInfoExample();
        UserInfoExample.Criteria criteria = example.createCriteria();
        criteria.andUidEqualTo(user.getUid());
        UserInfo userInfo=userInfoMapper.selectByExample(example).get(0);
        log.setUiid(userInfo.getUiid());
        //调用service保存SysLog实体类到数据库
        logMapper.insert(log);

    }

    @Override
    public List<Log> logfindAll(String uname) {
        LogExample logExample=new LogExample();
        LogExample.Criteria criteria1 = logExample.createCriteria();

        List<Integer>uiids=new ArrayList<>();
        if (uname!=""&&uname!=null){
            UserInfoExample example=new UserInfoExample();
            UserInfoExample.Criteria criteria = example.createCriteria();
            criteria.andUnameLike("%"+uname+"%");
            List<UserInfo>userInfoList=userInfoMapper.selectByExample(example);
            if(userInfoList!=null){
                for (UserInfo userInfo : userInfoList) {
                    uiids.add(userInfo.getUiid());
                }
                criteria1.andUiidIn(uiids);
            }
        }
        List<Log> logList=logMapper.selectByExample(logExample);
        if (logList!=null){
            for (Log log : logList) {
                UserInfo userInfo=userInfoMapper.selectByPrimaryKey(log.getUiid());
                log.setUserInfo(userInfo);
            }
        }
        return logList;
    }


}
